﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CoreShop.App
{
    public partial class frmfoodfeed : UIForm
    {
        private List<corecmsfoodfeed> feedList = null;

        public frmfoodfeed()
        {
            InitializeComponent();
        }

        public void AddType(string type)
        {
            UIMenuItemSeparator item = new UIMenuItemSeparator(type, 40);            
            item.SetOtherMode();
            item.Width = uiFlowLayoutPanel2.Width - 30;
            uiFlowLayoutPanel2.Add(item);
        }

        public void AddFoods(UAddFoodItemInfo info)
        {
            UIAddFoodsItem item = new UIAddFoodsItem();
            item.Tag = info.ItemId;
            item.Type = info.Type;
            item.Food = info.Food;
            item.Money = info.Money;
            item.Count = info.Count;
            item.MaxCount = 0;
            uiFlowLayoutPanel2.Add(item);
        }

        private void frmfoodfeed_Load(object sender, System.EventArgs e)
        {
            lblFoodName.Text = "商品："+ (this.Owner as FOrdering).orderingSelected.name;
            feedList = new corecmsfoodfeedServices().QueryFromOrdering((this.Owner as FOrdering).orderingSelected.id, (this.Owner as FOrdering).orderingSelected.foodId,AppConstVars.sysStore.id);
            if (feedList.Count == 0)
            {
                ShowInfoTip("没有添加做法资料,无法选择。");
                btnOk.Visible = false;
                return;
            }
            string feedTypeName = "";
            foreach (var feed in feedList)
            {
                if (feedTypeName != feed.feedTypeName)
                {
                    feedTypeName = feed.feedTypeName;
                    AddType(feedTypeName);        
                }
                string price = string.Format("￥{0}", feed.price.ToString("0.##"));
                AddFoods(new UAddFoodItemInfo(feedTypeName, feed.name, price, feed.quantity, feed.id.ToString()));
            }
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            List<UAddFoodItemInfo> infos = new List<UAddFoodItemInfo>();
            foreach (var ctrl in this.GetControls<UIAddFoodsItem>(true))
            {
                if (ctrl.Count > 0)
                {
                    long id = CommFunc.ConvertToInt64(ctrl.Tag);
                    int count = ctrl.Count;
                    var feedSels = feedList.Where(p => p.id == id).ToList();
                    foreach (var feedSel in feedSels)
                    {
                        feedSel.selectedFlag = true;
                        feedSel.quantity = ctrl.Count;
                    }
                }
            }
            string sSelectTxt = "";
            foreach (var foodfeed in feedList)
            {
                if (foodfeed.selectedFlag)
                {
                    sSelectTxt = sSelectTxt + foodfeed.name;
                }
            }
            if (sSelectTxt.Length > 600)
            {
                UIMessageBox.Show("选择的数据过多。无法保存。", "保存失败", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }

            var bl = new corecmsfoodorderingServices().SaveOrderingFeed((this.Owner as FOrdering).orderingSelected, feedList);
            if (!bl) {
                UIMessageBox.Show("保存失败。", "系统出错-SaveOrderingFeed:", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
